<template>
  <div id="pictureDetailPage">
    <a-row :gutter="[16, 16]">
      <!-- 图片预览 -->
      <a-col :sm="24" :md="16" :xl="18">
        <a-card title="图片预览">
          <a-image :src="picture.url" style="max-height: 480px; object-fit: contain" />
        </a-card>
      </a-col>
      <!-- 图片信息区域 -->
      <a-col :sm="24" :md="8" :xl="6">
        <a-card title="图片信息">
          <a-descriptions :column="1">
            <a-descriptions-item label="作者">
              <a-space>
                <a-avatar :src="picture.user?.userAvatar" size="small" />
                <div>{{ picture.user?.username }}</div>
              </a-space>
            </a-descriptions-item>

            <a-descriptions-item label="名称">
              {{ picture.name ?? '未命名' }}
            </a-descriptions-item>

            <a-descriptions-item label="简介">
              {{ picture.introduction ?? '-' }}
            </a-descriptions-item>

            <a-descriptions-item label="分类">
              {{ picture.category ?? '默认' }}
            </a-descriptions-item>

            <a-descriptions-item label="标签">
              <a-tag v-for="tag in picture.tags" :key="tag">
                {{ tag }}
              </a-tag>
            </a-descriptions-item>

            <a-descriptions-item label="格式">
              {{ picture.picFormat ?? '-' }}
            </a-descriptions-item>

            <a-descriptions-item label="宽度">
              {{ picture.picWidth ?? '-' }}
            </a-descriptions-item>

            <a-descriptions-item label="高度">
              {{ picture.picHeight ?? '-' }}
            </a-descriptions-item>

            <a-descriptions-item label="宽高比">
              {{ picture.picScale ?? '-' }}
            </a-descriptions-item>

            <a-descriptions-item label="图片大小">
              {{ formatSize(picture.picSize) }}
            </a-descriptions-item>

            <a-descriptions-item label="主色调">
              {{ picture.picColor ?? '-' }}
              <div
                :style="{
                  width: '16px',
                  height: '16px',
                  backgroundColor: toHexColor(picture.picColor),
                }"
              />
            </a-descriptions-item>
          </a-descriptions>
          <!-- 图片操作区域 -->
          <a-space wrap>
            <a-button type="primary" @click="doDownload"
              >免费下载
              <template #icon>
                <DownloadOutlined />
              </template>
            </a-button>
            <a-button @click="doShare" type="primary" ghost>
              <ShareAltOutlined />
              分享
            </a-button>
            <a-button v-if="canEdit" :icon="h(EditOutlined)" type="default" @click="doEdit"
              >编辑
            </a-button>
            <a-button v-if="canDelete" :icon="h(DeleteOutlined)" danger @click="doDelete"
              >删除
            </a-button>
          </a-space>
        </a-card>
      </a-col>
    </a-row>
    <ShareModal ref="shareModalRef" :link="shareLink" />
  </div>
</template>
<script setup lang="ts">
import { computed, h, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { deletePictureUsingPost, getPictureVoByIdUsingGet } from '@/api/pictureController'
import { message } from 'ant-design-vue'
import { downloadImage, formatSize, toHexColor } from '@/utils'
import {
  DeleteOutlined,
  DownloadOutlined,
  EditOutlined,
  ShareAltOutlined,
} from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import ShareModal from '@/components/ShareModal.vue'
import {SPACE_PERMISSION_ENUM} from "@/constants/space";

const loading = ref(true)

interface props {
  id: string | number
}

const props = defineProps<props>()
const picture = ref<API.PictureVO>({})
const route = useRoute()
// 获取老数据
const fetchPictureDetail = async () => {
  try {
    // 获取到id
    const res = await getPictureVoByIdUsingGet({
      id: props.id,
    })
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
    } else {
      message.error('获取图片详细失败.' + res.data.message)
    }
  } catch (e: any) {
    message.error('获取图片详细失败.' + e.message)
  }
}

// 页面加载时加载数据
onMounted(() => {
  fetchPictureDetail()
})

const loginUserStore = useLoginUserStore()
// const canEdit = computed(() => {
//   const loginUser = loginUserStore.loginUser
//   // 未登录不可编辑
//   if (!loginUser.id) {
//     return false
//   }
//   // 仅管理或本人可编辑
//   const user = picture.value.user || {}
//   return loginUser.id === user.id || loginUser.userRole === 'admin'
// })

// 编辑操作
const router = useRouter()
const doEdit = () => {
  // router.push('/add_picture?id=` + `${picture.value.id}')
  router.push({
    path: '/add_picture',
    query: {
      id: picture.value.id,
      spaceId: picture.value.spaceId,
    },
  })
}

// 删除操作
const doDelete = async () => {
  const id = picture.value.id
  if (!id) {
    return
  }
  const res = await deletePictureUsingPost({
    id,
  })

  if (res.data.code === 0) {
    message.success('删除成功')
  } else {
    message.error('删除失败. ' + res.data.message)
  }
}

// 下载操作
const doDownload = () => {
  downloadImage(picture.value.url, picture.value.name)
}

// -------------- 分享操作 --------------
const shareModalRef = ref()
const shareLink = ref<string>()
const doShare = async () => {
  shareLink.value = `${window.location.protocol}//${window.location.host}/picture/${picture.value.id}`
  if (shareModalRef) {
    shareModalRef.value.openModal()
  }
}

// 通用权限检查函数
function createPermissionChecker(permission: string) {
  return computed(() => {
    return (picture.value.permissionList ?? []).includes(permission)
  })
}

// 定义权限检查
const canEdit = createPermissionChecker(SPACE_PERMISSION_ENUM.PICTURE_EDIT)
const canDelete = createPermissionChecker(SPACE_PERMISSION_ENUM.PICTURE_DELETE)

</script>

<style scoped>
#pictureDetailPage {
  margin-bottom: 16px;
}
</style>
